package com.seventh.parkingrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.dao.ParkingSpaceMapper;
import com.seventh.parkingrent.domain.RentMessage;
import com.seventh.parkingrent.service.IParkingSpaceService;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 车位信息表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Service
@Slf4j
public  class ParkingSpaceServiceImpl extends ServiceImpl<ParkingSpaceMapper, ParkingSpace> implements IParkingSpaceService{


   @Autowired
  ParkingSpaceMapper parkingSpaceMapper;
   @Autowired
   RentMessageServiceImpl rentMessageService;



    @Override
    public ParkingSpace findStatus(int parkingSpaceId) {
        ParkingSpace parkingSpace=parkingSpaceMapper.findStatus(parkingSpaceId);
        return  parkingSpace;
    }

    @Override
    public IPage getParkingSpace(int pageIndex, int pageSize) {
        //分页参数
        IPage page = new Page(pageIndex,pageSize);

        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        IPage pageresult = parkingSpaceMapper.selectPage(page, wrapper);
        return pageresult;
    }

    @Override
    public ParkingSpace getParkingSpaceByid(Integer id) {
       return parkingSpaceMapper.selectById(id);
    }

    @Override
    public Boolean addParkingSpace(ParkingSpace parkingSpace) {
        Integer a=parkingSpaceMapper.insert(parkingSpace);

        return a!=null;
    }



    @Override
    public IPage<ParkingSpace> findAll(int userId,int current) {

        IPage<ParkingSpace> page = new Page(current,4);

        IPage page1= parkingSpaceMapper.findAll(page,userId);

        return page1;
    }

    @Override
    public Integer changeState(String state,int parkingSpaceId) {

        Integer res=parkingSpaceMapper.changeState(state,parkingSpaceId);
        return res;



    }


    @Override
    public List<ParkingSpace> findAllByNo(String parkingSpaceNo) {

        List<ParkingSpace> list= parkingSpaceMapper.checkNo( parkingSpaceNo);
        return  list;

    }



    @Override
    public ParkingSpace findParkingSpaceById(int parkingSpaceId) {

        ParkingSpace parkingSpace=parkingSpaceMapper.selectById(parkingSpaceId);

        return parkingSpace;
    }

    @Override
    public Integer delParkingSpace(int parkingSpaceId) {

        Integer i = parkingSpaceMapper.deleteById(parkingSpaceId);

        return i;
    }

    @Override
    public Integer updateStatusById(Integer rentMessageId) {
        ParkingSpace parkingSpace = parkingSpaceMapper.selectById(rentMessageId);
        parkingSpace.setState("3");
        int i = parkingSpaceMapper.updateById(parkingSpace);
        return i;
    }

    @Override
    public List<RentMessage> nearbyparking(Double x, Double y) {

        Map<Integer, Double> map = new HashMap<>();

        List<RentMessage> rents = rentMessageService.getAllNotRent();
        for (RentMessage rent : rents) {
            Integer id = rent.getSpacingSpaceId();
            ParkingSpace space = getParkingSpaceByid(id);
            double pointx = Double.parseDouble(space.getPointX());
            double pointy = Double.parseDouble(space.getPointY());
            double count = Math.pow((x - pointx), 2) + Math.pow((y - pointy), 2);
            map.put(id,count);
        }
        List<Map.Entry<Integer,Double>> collet=map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toList());

        ArrayList<RentMessage> rentMessages = new ArrayList<>();
        for (Map.Entry<Integer, Double> entry : collet) {
            Integer id = entry.getKey();
            log.info("得到停车场id{}",id);
            List<RentMessage> rentMessages1 = rentMessageService.selectRentByPSId(id);
            for (RentMessage rentMessage : rentMessages1) {
                rentMessages.add(rentMessage);
            }
        }
//        Optional<Double> first = map.values().stream().sorted().findFirst();
//        if(!first.isPresent()){
//            return null;
//        }
//        Double minVal = first.get();
//        AtomicInteger minKey = new AtomicInteger(-1);
//        map.forEach((key,value)->{
//            if (value.equals(minVal)){
//                minKey.set(key);
//            }
//        });
//        int i = minKey.get();
        return rentMessages;
    }
}
